package com.zou.baidu_music_radio;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.zou.baidu_music_radio.adapter.MyAdapter;
import com.zou.baidu_music_radio.bean.Hotsongsbean;
import com.zou.baidu_music_radio.utils.BMA;
import com.zou.baidu_music_radio.utils.MyParser;
import com.zou.baidu_music_radio.utils.SPUtils;
import com.zou.baidu_music_radio.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {


    private ImageView    iv_title;
    private RecyclerView rcv;
    Hotsongsbean hotsongsbean;
    List<Hotsongsbean.SongListEntity> datas = null;
    String    regeJson;
    SPUtils   spUtils;
    MyAdapter myAdapter;
    private ToastUtil toastUtil;
    private String    jsonFromSp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initView();
        initData();

    }

    private void initView() {
        iv_title = (ImageView) findViewById(R.id.iv_title);
        rcv = (RecyclerView) findViewById(R.id.rcv);
        spUtils = new SPUtils(MainActivity.this);
        toastUtil = new ToastUtil(MainActivity.this);

        checkData();

        rcv.setLayoutManager(new LinearLayoutManager(this));
        rcv.setBackgroundColor(Color.GRAY);
        myAdapter = new MyAdapter(datas, MainActivity.this);
        rcv.setAdapter(myAdapter);



    }

    //    检查sp中是否有数据
    private void checkData() {
        jsonFromSp = spUtils.getString(Constants.JSONSTRING, null);
        if (jsonFromSp != null) {
            datas = MyParser.parse(jsonFromSp).getSong_list();
            toastUtil.showToast("data From SP" + datas.get(0).getTitle(), 1);
        } else {
            // TODO: 2016/3/4  显示loding dialog
            datas = new ArrayList<>();
            toastUtil.showToast("显示loding dialog", 1);
        }
    }


    private void initData() {
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        StringRequest stringRequest = new StringRequest
                (BMA.HOTSONGS
                        , new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        regeJson = response;
//                        处理json数据
                        spUtils.putString(Constants.JSONSTRING, response);


                        hotsongsbean = MyParser.parse(regeJson);
                        toastUtil.showToast(hotsongsbean.getSong_list().get(0).getTitle(), 0);

                        //新的数据和旧的数据指向不同的堆内存，所以不能直接赋值，而应该使用addAll
                        List<Hotsongsbean.SongListEntity> temp = hotsongsbean.getSong_list();
                        datas.clear();
                        datas.addAll(temp);
                        //通知数据更新
                        myAdapter.notifyDataSetChanged();
                        // TODO: 2016/3/4 移除loading dialog
                        toastUtil.showToast("移除loding dialog" + datas.get(0).getTitle(), 1);
                    }
                }
                        , new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO: 2016/3/2 显示加载失败
                        Toast.makeText(MainActivity.this, "加载数据失败", Toast.LENGTH_SHORT).show();
                    }
                });
        requestQueue.add(stringRequest);
    }

}
